<?php

namespace App\Http\Controllers\Store\Sponsor;

use App\Exceptions\SponsorException;
use App\Services\Store\Sponsor\SponsorService;
use Exception;
use Illuminate\Http\JsonResponse;

class SponsorController extends BaseSponsorController
{
    protected SponsorService $sponsorService;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->sponsorService = $this->loadService('Store\Sponsor\SponsorService');
    }

    /**
     * 牧场列表
     * @return JsonResponse
     * @throws Exception
     */
    public function list(): JsonResponse
    {
        $rule = [
            'ranchId' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $list = $this->sponsorService->list($this->params);
        return outputJsonResult($list);
    }

    /**
     * 牧场详情
     * @return JsonResponse
     * @throws SponsorException
     */
    public function detail(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $detail = $this->sponsorService->detail($this->params);
        return outputJsonResult($detail);
    }

    /**
     * 快递公司列表
     * @return JsonResponse
     * @throws Exception
     */
    public function expressCompanyList(): JsonResponse
    {
        $list = $this->sponsorService->expressCompanyList();
        return outputJsonResult($list);
    }

    /**
     * 上传图片
     * @return JsonResponse
     * @throws Exception
     */
    public function upload(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
            'imgUrl' => 'required|string'
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->sponsorService->update($this->params);
        return outputJsonResult();
    }

    /**
     * 发放积分
     * @return JsonResponse
     * @throws Exception
     */
    public function grantIntegral(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
            'integral' => 'required|integer'
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->sponsorService->grantIntegral($this->params);
        return outputJsonResult();
    }

    /**
     * 发放快递
     * @return JsonResponse
     * @throws Exception
     */
    public function createExpress(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer|min:1',
            'expressCompanyCode' => 'required|string|min:1',
            'expressNo' => 'required|string'
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->sponsorService->createExpress($this->params);
        return outputJsonResult();
    }
}
